package com.xydtech.common.number;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Title: MoneyUtil
 * @Package: com.xydtech.common.number
 * @Description: 钱相关工具类
 * @date: 2021-06-09 16:21
 * @version: V1.0
 **/
public class MoneyUtil {
    /**
     * @title 获取格式化的人民币（四舍五入）
     * @author chanson
     * @param money  待处理的人民币
     * @param scale  小数点后保留的位数
     * @param divisor 格式化值（万，百万，亿等等）
     * @return
     */
    public static String getFormatMoney(double money, int scale, double divisor) {
        if (divisor == 0) {
            return "0.00";
        }
        if (scale < 0) {
            return "0.00";
        }
        BigDecimal moneyBD = new BigDecimal(money);
        BigDecimal divisorBD = new BigDecimal(divisor);
        return moneyBD.divide(divisorBD, scale, RoundingMode.HALF_UP).toString();
    }

    /**
     * @title 获取会计格式的人民币（四舍五入）——添加会计标识：','
     * @author chanson
     * @param money  待处理的人民币
     * @param scale  小数点后保留的位数
     * @param divisor 格式化值（万，百万，亿等等）
     * @return
     */
    public static String getAccountantMoney(double money, int scale, double divisor) {
        String disposeMoneyStr = getFormatMoney(money, scale, divisor);
        //小数点处理
        int dotPosition = disposeMoneyStr.indexOf(".");
        //小数点之前的字符串
        String exceptDotMoeny = null;
        //小数点之后的字符串
        String dotMeony = null;
        if (dotPosition > 0) {
            exceptDotMoeny = disposeMoneyStr.substring(0, dotPosition);
            dotMeony = disposeMoneyStr.substring(dotPosition);
        } else {
            exceptDotMoeny = disposeMoneyStr;
        }
        //负数处理
        int negativePosition = exceptDotMoeny.indexOf("-");
        if (negativePosition == 0) {
            exceptDotMoeny = exceptDotMoeny.substring(1);
        }
        StringBuffer reverseExceptDotMoney = new StringBuffer(exceptDotMoeny);
        //字符串倒转
        reverseExceptDotMoney.reverse();
        char[] moneyChar = reverseExceptDotMoney.toString().toCharArray();
        //返回值
        StringBuffer returnMeony = new StringBuffer();
        for (int i = 0; i < moneyChar.length; i++) {
            if (i != 0 && i % 3 == 0) {
                //每隔3位加','
                returnMeony.append(",");
            }
            returnMeony.append(moneyChar[i]);
        }
        //字符串倒转
        returnMeony.reverse();
        if (dotPosition > 0) {
            returnMeony.append(dotMeony);
        }
        if (negativePosition == 0) {
            return "-" + returnMeony.toString();
        } else {
            return returnMeony.toString();
        }
    }

    public static void main(String[] args) {
        double money = -1269486459.86;
        int scale = 2;
        double divisor = 10000.00;
        System.out.println("原货币值: " + money);
//      System.out.println("货币值: "+util.getAccountantMoney(money, scale, 1));
        String formatMeony = MoneyUtil.getFormatMoney(money, scale, divisor);
        System.out.println("格式化货币值: " + formatMeony + "万元");
        String accountantMoney = MoneyUtil.getAccountantMoney(money, scale, divisor);
        System.out.println("会计货币值: " + accountantMoney + "万元");
    }
}